<template>
  <div class="body"
       v-loading="realFlowLoading"
       :element-loading-text="otherI18n.Loading" >
    <bpm-d-navbar @save="save"
                  @submitVerify="submitVerify"
                  @confirmVerify="confirmVerify"
                  @rejectVerify="rejectVerify"
                  @rejectConfirmVerify="rejectConfirmVerify"
                  @reassignVerify="reassignVerify"/>
    <bpm-d-diagram v-show="diagramVisible"/>
    <bpm-d-history v-show="historyVisible"/>
    <!--业务实现-->
    <div v-show="mainVisible">
      <bpm-d-basearea :title="getLanguage('title.expenseReimburseProcess')" />
      <bpm-d-businessarea />
      <bpm-d-attachframe :allowCreate="true"
                         :allowUpdate="true"
                         :allowDelete="true"
                         :allowUpdateOthers="false"
                         :allowDeleteOthers="false"/>
    </div>
  </div>
</template>

<script>
import {mapState, mapActions} from 'vuex'

export default {
  name: 'MainScene',
  data () {
    return {
      flowLoading: false
    }
  },
  computed: {
    ...mapState(['hsInput', 'flowTypeEnum', 'pageTypeEnum', 'lang', 'langEnum', 'bpmcoreI18n', 'otherI18n']),
    ...mapState(['bizData', 'mainVisible', 'diagramVisible', 'historyVisible', 'pageType', 'pageTypeEnum']),
    ...mapState(['ssc', 'lang', 'apiloading']),
    ...mapState(['inst', 'bpmcoreI18n']),
    ...mapState(['userId', 'tsId', 'dictFields', 'viewTableVisible', 'view1stMounted']),
    realFlowLoading () {
      return this.flowLoading || (this.apiloading > 0)
    },
    chailvRowGridInfo () {
      console.log('chailvrow payment grid item', this.$store.state.chailvRow.ssc.reimburse.whBtReimburseDdSub)
      return this.$store.state.chailvRow.ssc.reimburse.whBtReimburseDdSub
    }
  },
  created () {
    // 更新费用报销页面类型
    this.setPageType(this.pageTypeEnum.draft)
  },
  mounted () {
    // 通用加载服务
    console.log('URL======', window.location.href)
    this.loadResource({me: this})
    if (this.pageType !== this.pageTypeEnum.draft) { return }
    if (this.view1stMounted) {
      return
    }
    console.log('setView1stMounted', 2)
    this.setView1stMounted(true)
    this.$nextTick(function () {
      console.log('whole view mounted........')
      this.flowLoading = true
      setTimeout(() => {
        console.log('all needed ajax asked here for draft………………………………………………')
        // 取传递进来的tsId
        if (this.$store.state.hsInput.ts_piid === '-1') {
          this.getDataSscReimburseByUserId({me: this})
        } else {
          let paramJson = {}
          paramJson.me = this
          paramJson.tableNames = 'WH_BT_FAREDETAILS,WH_BT_STAYDETAILS,WH_BT_TRANSPORTATIONDETAILS,WH_BT_TRAVELSUBSIDYDETAILS,WH_BT_REIMBURSE_COSTSHARE,WH_BT_REIMBURSE_DD_SUB,WH_BT_REIMBURSE'
          paramJson.BB = 'Y'
          this.getDataSscReimburseFromMoreTable(paramJson)
        }
        this.getStaffLoan({
          me: this,
          async: true
        })
        console.log('all needed ajax asked here^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
        this.flowLoading = false
      }, 100)
    })
  },
  methods: {
    ...mapActions(['loadResource', 'setView1stMounted']),
    ...mapActions(['setPageType']),
    ...mapActions(['setBizData', 'setIsReassignVerify', 'setIsSubmitVerify', 'setIsConfirmVerify', 'setIsRejectVerify', 'setIsRejectConfirmVerify', 'saveTableWh_bt_reimburse', 'draftSubmitService']),
    ...mapActions(['getDataSscReimburseByUserId', 'getDataSscReimburseWhBtReimburse', 'queryBBOptions', 'getDataSscReimburseFromMoreTable']),
    ...mapActions(['getDataFromTableDdSub', 'queryInternalOrder', 'draftCallAjaxCheckBeforeSubmit']),
    ...mapActions(['getDataFromTableCostshare', 'getStaffLoan']),
    // 点击提交按钮出发的方法
    submitVerify () {
      this.setIsSubmitVerify(false)
      console.log('submitVerify start- >')
      console.log('reimburse.whBtReimburse submitVerify need save btno', !this.$store.state.ssc.reimburse.whBtReimburse.btNo)
      if (!this.$store.state.ssc.reimburse.whBtReimburse.btNo) {
        this.$store.state.ssc.reimburse.whBtReimburse.btNo = this.$store.state.inst.btNo
      }
      let pageNum = this.$store.state.ssc.reimburse.whBtReimburse.folio // A4纸张数
      let totalsum = this.$store.state.ssc.reimburse.whBtReimburse.totalsum // 合计金额
      let reimCurrency = this.$store.state.ssc.reimburse.whBtReimburse.reimbStandardMoney // 报销币别
      let apifinished = this.$store.state.apifinished // api 调用已结束 计数
      if (apifinished > 0) {
        this.alert(this.getResource('alert.apidyswjs')) // "API调用尚未结束，不能提交!"
        return false
      }

      if (pageNum == null || pageNum * 1 <= 0) {
        this.alert(this.getResource('alert.A4foliobnwk')) // "A4纸张数不能为0!"
        return false
      }
      if (totalsum == null || totalsum * 1 === 0) {
        this.alert(this.getResource('alert.information.jehjbnwk'))// "金额合计不能为空_,且不能为0!"
        return false
      }
      if (reimCurrency == null || reimCurrency === '' || reimCurrency === '------') {
        this.alert(this.getResource('alert.information.bbbnwk'))// 报销币别不能为空！
        return false
      }
      // 先保存主表数据20180521
      this.$store.state.ssc.reimburse.whBtReimburse.tsId = this.$store.state.tsId
      this.saveTableWh_bt_reimburse({
        me: this,
        data: this.$store.state.ssc.reimburse.whBtReimburse
      })
      let applyDate = this.toLocaleDate(this.inst.createTime)
      console.log('this.$store.state.inst===localDate==', applyDate)
      this.draftCallAjaxCheckBeforeSubmit({
        me: this,
        data: 'draft',
        applyDate: applyDate
      })
      if (this.$store.state.retMsg === 'SAPERR') {
        this.alert(this.$store.state.retMsg)
        this.$store.state.retMsg = ''
        return false
      } else if (this.$store.state.retMsg !== '' && this.$store.state.retMsg !== null) {
        this.alert(this.getResource(this.$store.state.retMsg))
        this.$store.state.retMsg = ''
        return false
      }
      let obj = {}
      obj.totalsum = totalsum
      this.setBizData(JSON.stringify(obj)) // 根据实际业务传输金额
      if (this.$store.state.ssc.reimburse.whBtReimburse.isscan === 'Y') {
        this.alert(this.getResource('alert.scanHint')).then(res => {
          console.log('submitVerify end- >')
          this.setIsSubmitVerify(true)
        })
      } else {
        this.setIsSubmitVerify(true)
      }
    },
    // 点击提交弹框中的确认按钮出发的方法
    confirmVerify () {
      this.setIsConfirmVerify(false)
      console.log('confirmVerify 校验业务数据开始- >')
      // todo 业务校验
      this.draftSubmitService({
        me: this,
        data: this.$store.state.ssc.reimburse.whBtReimburse,
        applyDate: this.toLocaleDate(this.inst.createTime)
      })
      if (this.$store.state.retMsg === 'SAPERR') {
        this.alert(this.$store.state.retMsg)
        this.$store.state.retMsg = ''
        return false
      } else if (this.$store.state.retMsg !== '' && this.$store.state.retMsg !== null) {
        this.alert(this.getResource(this.$store.state.retMsg))
        this.$store.state.retMsg = ''
        return false
      }
      console.log('confirmVerify 校验业务数据结束- >')
      this.setIsConfirmVerify(true)
    },
    // 点击驳回按钮触发的方法
    rejectVerify () {
      this.setIsRejectVerify(false)
      console.log('rejectVerify 校验业务数据开始- >')
      // todo 业务校验
      console.log('rejectVerify 校验业务数据结束- >')
      this.setIsRejectVerify(true)
    },
    // 点击驳回弹框中的确认按钮触发的方法
    rejectConfirmVerify () {
      this.setIsRejectConfirmVerify(false)
      console.log('rejectConfirmVerify 校验业务数据开始- >')
      // todo 业务校验
      console.log('rejectConfirmVerify 校验业务数据结束- >')
      this.setIsRejectConfirmVerify(true)
    },
    // 点击加签按钮触发的方法
    reassignVerify () {
      this.setIsReassignVerify(false)
      console.log('reassignVerify 校验业务数据开始- >')
      // todo 业务校验
      console.log('reassignVerify 校验业务数据结束- >')
      this.setIsReassignVerify(true)
    },
    // 点击保存按钮触发的方法
    save () {
      console.log('save 开始- >')
      // 保存前格式化数据
      this.$store.state.ssc.reimburse.whBtReimburse.tsId = this.$store.state.tsId
      this.saveTableWh_bt_reimburse({
        me: this,
        data: this.$store.state.ssc.reimburse.whBtReimburse
        // data: this.ssc.reimburse.whBtReimburse
      })
      // todo 业务校验
      console.log('save 结束- >')
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .body {
    display: flex;
    flex-direction: column;
    font-size: 12px;
  }
</style>
